package com.bw.controller.gy;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.entity.material.PiMaterial;
import com.bw.entity.orders.PiOrder;
import com.bw.entity.orders.PiOrderItem;
import com.bw.req.GyVO;
import com.bw.resp.ResponseResult;
import com.bw.service.orders.IPiOrderItemService;
import com.bw.service.orders.IPiOrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wuhao
 * @date 2020-06-22 19:50
 */
@RestController
@RequestMapping("/gy")
public class GyController {

    Logger logger= LoggerFactory.getLogger(GyController.class);

    @Resource
    private IPiOrderService iPiOrderService;

    @Resource
    IPiOrderItemService iPiOrderItemService;

    /**
     * @Description:列表分页
     * @Author: 周明丽
     * @Date: 2020/7/1 19:10
     * @param currentPage:
     * @param pageSize:
     * @param piOrder:
     * @return: com.bw.resp.ResponseResult
     **/
    @PostMapping("list/{currentPage}/{pageSize}")
    public ResponseResult list(@PathVariable("currentPage")int currentPage, @PathVariable("pageSize")int pageSize, @RequestBody PiOrder piOrder){
        logger.info("查询工艺开始");
        ResponseResult result=ResponseResult.SUCCESS();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.orderByDesc("updateDate");
        try {
            PageHelper.startPage(currentPage,pageSize);
            if(piOrder.getCode() !=null ){
                wrapper.like("userName",piOrder.getCode());
            }

            List<GyVO> list=iPiOrderService.findGy(piOrder);
            PageInfo pageInfo=new PageInfo(list);
            result.setResult(pageInfo);
            logger.info("查询工艺列表成功"+list.toString());
        } catch (Exception e) {
            logger.error("查询失败"+e.toString());
            result.setSuccess(false);
            result.setMessage("查询失败"+e.toString());
        }
        return result;
    }

    /**
     * @Description:查询工艺信息
     * @Author: 周明丽
     * @Date: 2020/7/1 19:10
     * @param id:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("getById/{id}")
    public ResponseResult getById(@PathVariable("id") Long id) {
        logger.info("BOM开始"+id);
        ResponseResult result = ResponseResult.SUCCESS();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("orderId",id);
        try {
            List<PiOrderItem> piOrderItem = iPiOrderItemService.list(queryWrapper);
            result.setResult(piOrderItem);
            result.setMessage("查询BOM成功");
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询BOM失败:" + e.toString());
        }
        logger.info("BOM结束");
        return result;
    }

    /**
     * @Description:修改状态
     * @Author: 周明丽
     * @Date: 2020/7/1 19:10
     * @param id:
     * @return: com.bw.resp.ResponseResult
     **/
    @GetMapping("find1/{id}")
    public  ResponseResult find(@PathVariable("id") long id){
        ResponseResult result=ResponseResult.SUCCESS();
        logger.info("开始操作");
        try {
            System.out.println(id);
            PiOrder order=iPiOrderService.getById(id);
            order.setState(2);//提交
            System.out.println(order);
            iPiOrderService.saveOrUpdate(order);
            logger.info("修改成功");
            result.setMessage("修改成功");
        }catch (Exception e){
            logger.error("修改失败"+e.toString());
            result.setMessage("修改失败"+e.toString());
        }
        return  result;
    }
}
